package priv.xp.androidexample

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.alibaba.android.arouter.facade.annotation.Route
import com.google.zxing.integration.android.IntentIntegrator
import priv.xp.androidexample.databinding.FragmentQrcodeBinding


@Route(path = "/qrcode/fragment")
class QrCodeFragment : Fragment() {
    private lateinit var binding: FragmentQrcodeBinding

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentQrcodeBinding.inflate(inflater, container, false)

        binding.fab.setOnClickListener {
            IntentIntegrator.forSupportFragment(this).initiateScan()
        }
        return binding.root
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        val result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data)

        result?.contents?.let {
            binding.txQrCode.text = it
        }

        return super.onActivityResult(requestCode, resultCode, data)
    }

}
